﻿using Artech.PetShop.Common;
using Artech.PetShop.Orders.BusinessEntity;
using Artech.PetShop.Orders.DataAccess;
using Artech.PetShop.Products.Service.Interface;
using Microsoft.Practices.Unity;
namespace Artech.PetShop.Orders.BusinessComponent
{
    public class OrderBC:BusinessComponentBase
    {
        [Dependency]
        public OrderDA DataAccess
        { get; set; }

        [Dependency]
        public IProductService ProductService
        { get; set; }

        private void ValidateInventory(Order order)
        {
            foreach (var detail in order.Details)
            {            

                if(this.ProductService.GetInventory(detail.ProductID) < detail.Quantity)
                {
                    throw new BusinessException("Lack of stock!");
                }
            }
        }

        public void Submit(Order order)
        {
            this.ValidateInventory(order);
            this.DataAccess.Submit(order);
        }
    }
}